var myApp = new Framework7();
//发送验证码点击事件
$(".submit_1").on('click',function () {
    var phone=$.trim($("#phone").val());
    if(phone==""){
        myApp.alert('','手机号不能为空');
    }else{
        phoneCheck(phone);
    }
});
//点击注册按钮事件
$(".submit").on("click",function () {
    var name=$.trim($("#name").val());
    var phone=$.trim($("#phone").val());
    var verifyCode=$.trim($("#verifyCode").val());
    var pwd_1=$.trim($("#pwd_1").val());
    var pwd_2=$.trim($("#pwd_2").val());
    var flg=0;
    if(name==""){
        flg=0;
        myApp.alert('','请输入姓名');
    }else{
        if(phone==""){
            flg=0;
            myApp.alert('','请输入手机号码');
        }else{
            if(verifyCode==""){
                flg=0;
                myApp.alert('','请输入验证码');
            }else{
                if(pwd_1==""){
                    flg=0;
                    myApp.alert('','请输入密码');
                }else{
                    if(pwd_2==""){
                        flg=0;
                        myApp.alert('','请输入确认密码');
                    }else{
                        if(pwd_1!=pwd_2){
                            flg=0;
                            myApp.alert('','密码不一致');
                        }else{
                            flg=1;
                        }
                    }
                }
            }
        }
    }
    if(flg==1){
        register(name,phone,verifyCode,pwd_1);
    }
});
//手机号校验
function phoneCheck(phone) {
    var params=JSON.stringify({"phoneNumber":phone,"judgeCode":"0"});
    $.ajax({
        // url:'http://125.65.40.171/conference-check/conventionerController/matchPhoneNumber',
        url:'conventionerController/matchPhoneNumber',
        dataType:'json',
        data:{"param":params},
        type:"post",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success:function (data) {
            if(data.code==200){
                sendMessage(phone);
            }else if(data.code==500){
                myApp.alert('','手机号格式不正确');
            }else if(data.code==501){
                myApp.alert('','手机号已注册');
            }
        },
        error:function (XMLHttpRequest,textStatus,errorThrown) {
            myApp.alert('','系统错误，请稍后重试');
        }
    })
}
//发送验证码
function sendMessage(phone) {
    var params=JSON.stringify({"phoneNumber":phone});
    $.ajax({
        // url:'http://125.65.40.171/conference-check/messageController/sendRegisterVerificationCode',
        url:'messageController/sendRegisterVerificationCode',
        dataType:'json',
        data:{"param":params},
        type:"post",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success:function (data) {
            if(data.code==200){
                Countdown($(".submit_1"));
            }else if(data.code==500){
                myApp.alert('','短信发送失败');
            }
        },
        error:function (XMLHttpRequest,textStatus,errorThrown) {
            myApp.alert('','系统错误，请稍后重试');
        }
    })
}
//注册
function register(name,phone,verifyCode,pwd_1) {
    var params=JSON.stringify({"name":name,"phoneNumber":phone,"verificationCode":verifyCode,"password":pwd_1});
    $.ajax({
        // url:'http://125.65.40.171/conference-check/conventionerController/register',
        url:'conventionerController/register',
        dataType:'json',
        data:{"param":params},
        type:"post",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success:function (data) {
            if(data.code==200){
                window.location.href="./";
            }else if(data.code==500){
                myApp.alert('','注册失败');
            }else if(data.code==501){
                myApp.alert('','请输入正确的手机号码');
            }else if(data.code==502){
                myApp.alert('','验证码错误');
            }
        },
        error:function (XMLHttpRequest,textStatus,errorThrown) {
            myApp.alert('','系统错误，请稍后重试');
        }
    })
}
/*验证码读秒*/
var wait=60;
function Countdown(ele) {
    if(wait==0){
        ele.removeAttr("disabled");
        ele.text("发送验证码");
        wait = 60;
    }else{
        wait--;
        ele.attr("disabled",true);
        ele.text(wait+"秒后重新发送");
        setTimeout(function () {
            Countdown(ele);
        },1000)
    }
}